package com.hunan.api.catv.service.biz.tag;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.api.catv.common.CacheName;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.po.Tag;
import com.hunan.api.catv.entity.vo.TagVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.tag_content.TagContentBiz;
import com.hunan.api.catv.service.impl.tag.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
@CacheConfig(cacheNames = CacheName.tag)
public class TagBiz extends ResultService {

    @Autowired
    TagService tagService;

    @Autowired
    TagContentBiz tagContentBiz;

    public PageInfo<TagVo> adminPage(Integer pageNum, Integer pageSize, Integer columnId) {
        PageHelper.startPage(pageNum, pageSize);
        Tag tag = new Tag();
        tag.setColumnId(columnId);
        tag.setLevel(1);
        List<TagVo> list = tagService.list(tag);
        if (!ObjectUtils.isEmpty(list)) {
            list.forEach(tagVo -> {
                Integer tagId = tagVo.getTagId();
                tag.setParentId(tagId);
                tag.setLevel(2);
                List<TagVo> childList = tagService.list(tag);
                tagVo.setChildList(childList);
            });
        }
        return new PageInfo<TagVo>(list);
    }

    public ServiceResult add(Tag tag) {
        ServiceResult result = tagService.add(tag);
        if (result.getStatus()) {
            return buildOK(true, ConstantsMsg.admin_add_success);
        }
        return buildFail(false, ConstantsMsg.admin_add_fail);
    }

    public ServiceResult edit(Integer tagId, String name) {
        Tag tag = new Tag();
        tag.setTagId(tagId);
        tag.setName(name);
        ServiceResult result = tagService.update(tag);
        if (result.getStatus()) {
            return buildOK(true, ConstantsMsg.admin_edit_success);
        }
        return buildFail(false, ConstantsMsg.admin_edit_fail);
    }

    public ServiceResult del(Integer tagId, Integer level) {
        ServiceResult result = tagService.delete(tagId);
        if (result.getStatus()) {
            if (1 == level) {
                Tag tag = new Tag();
                tag.setParentId(tagId);
                result = tagService.batchDelete(tag);
                if (!result.getStatus()) {
                    return buildFail(false, ConstantsMsg.admin_del_fail);
                }
            }
            result = tagContentBiz.del(tagId, level);
            if (!result.getStatus()) {
                return buildFail(false, ConstantsMsg.admin_del_fail);
            }
            return buildOK(true, ConstantsMsg.admin_del_success);
        }
        return buildFail(false, ConstantsMsg.admin_del_fail);
    }

    public TagVo getById(Integer tagId) {
        return tagService.getById(tagId);
    }
}
